<template>
  <el-dialog
    @close="closeFn"
    @open="openFn"
    title="分配角色"
    :visible="visible"
  >
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeFn">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getRoleList } from "@/api/setting";
import { getUserDetail } from "@/api/user";
import { assignRole } from "@/api/employees";
export default {
  data() {
    return {
      checkList: [],
      list: [],
    };
  },
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    employeeId: {
      type: String,
      required: true,
    },
  },
  methods: {
    closeFn() {
      this.checkList = []
      this.$emit("update:visible", false);
    },
    async openFn() {
      this.getRoleList();
      this.getUserDetail();
    },
    async getRoleList() {
      const { rows } = await getRoleList();
      this.list = rows;
    },
    async getUserDetail() {
      const { roleIds } = await getUserDetail(this.employeeId);
      this.checkList = roleIds || [];
    },
    async submit() {
      if (!this.checkList.length) return this.$message.warning("请选择角色");
      await assignRole({
        id: this.employeeId,
        roleIds: this.checkList,
      });
      this.$message.success("分配角色成功了哦~");
      this.closeFn();
    },
  },
};
</script>